/*
 * Copyright (c) 2014. kupat Corporation. All rights reserved.
 *  see statement on http://www.kupat.cn.
 */
package com.kfgj.hkp.common.security.cryptography;

import com.kfgj.hkp.common.logging.Logger;
import com.kfgj.hkp.common.logging.LoggerFactory;
import com.kfgj.hkp.common.security.CryptoErrorCode;
import com.kfgj.hkp.common.security.CryptoException;
import com.kfgj.hkp.common.utils.CodingUtils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @author Shawn
 *
 */
public class MD5 {
//	static Log log = LogFactory.getLog(MD5.class);
	private static Logger logger = LoggerFactory.getLogger(MD5.class);

	public final static String SIGN_TYPE = "MD5";

	public static String md5(String str) {
		try {
			MessageDigest msgDigest = MessageDigest.getInstance(SIGN_TYPE);
			msgDigest.update(str.getBytes());
			byte[] enbyte = msgDigest.digest();
			return CodingUtils.byte2hex(enbyte);
		} catch (NoSuchAlgorithmException e) {
			logger.error(e.getMessage(), e);
			throw new CryptoException(CryptoErrorCode.Cry1002,e.getMessage(),e);
		}
	}
}
